//加载一级分类
loadCategory();

//加载推荐的分类
loadRecomCategory();

//搜索事件
$('#search').click(function () {
    var key = $('#key').val();
    window.location.href = "/search?key=" + key;
});

function loadRecomCategory() {
    obajax.get(
        '/api/category',
        {parentId: 0, recom: 1},
        (result) => {
            //渲染
            var html = ' ';

            $.each(result.data, (index, item) => {
                html +=
                    '                <div class="section-hd">\n' +
                    '                    <div class="title">' + item.name + '</div>\n' +
                    '                </div>\n' +
                    '                <div class="flash-sale clearfix" data-id="' + item.id + '">\n' +
                    '                </div>';
                loadRecomGoods(item.id);
            });
            $('.flash-sale-section').html(html);

        }
    );
}

//加载指定分类的推荐商品
function loadRecomGoods(categoryId) {
    //ajax
    obajax.get(
        '/api/goods',
        {categoryId, recom: 1},
        (result) => {
            var html = '';
            $.each(result.data, (index, item) => {
                html +=
                    '                    <div class="grid grid-s">\n' +
                    '                        <a href="/goods?id=' + item.id + '">\n' +
                    '                             <div class="inner-text">\n' +
                    '                                <div class="tit">' + item.name + '</div>\n' +
                    '                                <div class="stit">' + item.dscp + '</div>\n' +
                    '                                <div class="stit">' + item.price + '元起</div>\n' +
                    '                            </div>\n' +
                    '                            <img src="/upload/' + item.pics[0].url + '" alt="" />\n' +
                    '                        </a>\n' +
                    '                    </div>';
            })
            $('.flash-sale[data-id="' + categoryId + '"]').html(html);
        }
    );
}

function loadCategory() {
    //ajax
    obajax.get(
        '/api/category',
        {parentId: 0},
        (result) => {
            var html = '';
            //将数据渲染到页面中
            $.each(result.data, (index, category) => {
                html += '<div class="f-item">\n' +
                    '            <div class="f-box">\n' +
                    '                <div class="f-tit">' + category.name + '</div>\n' +
                    '                <div class="f-list">\n' +
                    '                    <p>' + category.dscp + '</p>\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    '            <div class="c-box">\n' +
                    '     <div class="hd">分类</div>\n' +
                '<div class="double">';

                $.each(category.children, (index, child) => {
                    html +=
                        '                    <div class="list-wrap">\n' +
                        '                    <img src="/upload/' + child.pic + '" width="35 px">\n' +
                        '                        <a href="/search?categoryId=' + child.id + '">' + child.name + '</a>\n' +
                        '                    </div>';

                });
                html +=
                    '         </div>\n' +
                    '         </div>\n' +
                    '    </div>';
            });
            $('.side-category-bd').html(html);
        }
    );
}